﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IBupro.Controllers.Classes;
using IBupro.Models;
using iBuproModel;
using iBuproModel.Repositories.Interfaces;

namespace IBupro.Controllers
{
    /// <summary>
    /// This controller handles general user business logic
    /// </summary>
    public class UserController : CustomController
    {
        private IUserRepository userRepository;

        public UserController()
        {
            userRepository = repositoryService.UserRepositoryCreateInstance();
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Index()
        {
            //IEnumerable<User> users = from u in userRepository.GetAll().ToList() select u;
            //return View(users);
            return View(this.userRepository.GetAll());
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Details(int id)
        {
            return View(this.userRepository.GetSingle(u => u.Id == id));
        }

        [Authorize(Roles = "Admin")]
        public ActionResult EditRoles(int id)
        {
            var user = userRepository.First(u => u.Id == id);
            var model = new UserModel(user);
            
            return View(model);
        }

        [Authorize(Roles = "Admin")]
        [HttpPost]
        public ActionResult EditRoles(int id, UserModel model)
        {
            try
            {
                
                if (ModelState.IsValid)
                {
                    var newUser = userRepository.GetSingle(u => u.Id == id);
                    newUser.Roles.Clear();

                    if (model.SelectedRoles != null)
                    {
                        userRepository.UpdateRoleList(newUser, model.SelectedRoles);
                    }

                    userRepository.SaveChanges();
                }
                else return View(model);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Edit(int id)
        {
            var user = userRepository.GetSingle(u => u.Id == id);
            var model = new UserModel(user);

            return View(model);
        }

        [Authorize(Roles = "Admin")]
        [HttpPost]
        public ActionResult Edit(int id, UserModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var oldUser = userRepository.GetSingle(u => u.Id == id);
                    oldUser.FirstName = model.User.FirstName;
                    oldUser.LastName = model.User.LastName;

                    if (model.SelectedRoles != null)
                    {
                        oldUser.Roles.Clear();
                        userRepository.UpdateRoleList(oldUser, model.SelectedRoles);
                    }

                    userRepository.Update(oldUser);
                    userRepository.SaveChanges();
                }
                else return View(model);

                return RedirectToAction("Index");
            }
            catch
            {
                return View(model);
            }
        }
    }
}
